package shuogg.dao;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import shuogg.domain.User;

/**
 * @description JdbcTemplate和web基础的JdbcUtils用法上基本一模一样
 *
 * @author linshuo
 *
 */
public class UserDao extends JdbcDaoSupport {

    public void update(User user) {
        String sql = "UPDATE t_user SET name=?, gender=?, age=? WHERE id=?";
        Object[] args = { user.getName(), user.getGender(), user.getAge(), user.getId() };
        this.getJdbcTemplate().update(sql, args);
    }

    public void insert(User user) {
        String sql = "INSERT INTO t_user VALUES(?,?,?,?)";
        Object[] args = { user.getId(), user.getName(), user.getGender(), user.getAge() };
        this.getJdbcTemplate().update(sql, args);
    }

    public void delete(Integer id) {
        String sql = "DELETE FROM t_user WHERE id=?";
        Object[] args = { id };
        this.getJdbcTemplate().update(sql, args);
    }

    public User getById(int id) {
        String sql = "select * from t_user where id = ?";
        Object[] args = { id };
        RowMapper<User> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(User.class);
        return this.getJdbcTemplate().queryForObject(sql, rowMapper, args);
    }
}
